package com.hotelsystem.reservations.layouts;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import com.hotelsystem.reservation.controllers.ReservationsListController;
import com.hotelsystem.reservations.reservations.Reservation;

public class ReservationListView {
	private DefaultListModel<Reservation> model = new DefaultListModel<Reservation>();
	private JList<Reservation> reservationsList = new JList<Reservation>(model);
	private JButton open, delete;
	private JToggleButton all, past, current, future;
	private JPanel mainPanel;
	

	public ReservationListView() {
		buildUI();
		ReservationsListController reservationsListController = new ReservationsListController(this);
		reservationsListController.loadAllReservations();
		
	}

	public void setReservationsList(JList<Reservation> reservationsList) {
		this.reservationsList = reservationsList;
	}

	public JToggleButton getAll() {
		return all;
	}


	public JToggleButton getPast() {
		return past;
	}

	public JToggleButton getCurrent() {
		return current;
	}

	public JToggleButton getFuture() {
		return future;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public JList<Reservation> getReservationsList() {
		return reservationsList;
	}

	public JButton getOpen() {
		return open;
	}

	public JButton getDelete() {
		return delete;
	}

	public void addListenerForOpen(ActionListener action) {
		open.addActionListener(action);
	}

	public void addListenerForDelete(ActionListener action) {
		delete.addActionListener(action);
	}

	private void buildUI() {
		mainPanel = new JPanel(new BorderLayout());
		
		JPanel toggleButtonsPanel = new JPanel(new FlowLayout());
		toggleButtonsPanel = buildToggles(toggleButtonsPanel);
		
		mainPanel.add(toggleButtonsPanel,BorderLayout.NORTH);
		
		mainPanel.add(reservationsList, BorderLayout.CENTER);
		
		mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		JPanel buttonsPanel = new JPanel(new FlowLayout());
		buttonsPanel = buildButtons(buttonsPanel);
		
		mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
	}

	private JPanel buildButtons(JPanel buttonsPanel) {		
		delete = new JButton("Delete");
		open = new JButton("Open");
		
		buttonsPanel.add(open);
		buttonsPanel.add(delete);
		buttonsPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
		
		return buttonsPanel;
	}

	private JPanel buildToggles(JPanel toggleButtonsPanel) {
		all = new JToggleButton("All");
		
		all.addActionListener(new ReservationsListController(this));
		all.setSelected(true);
		current = new JToggleButton("Current");
		current.addActionListener(new ReservationsListController(this));
		past = new JToggleButton("Past");
		past.addActionListener(new ReservationsListController(this));
		future = new JToggleButton("Future");
		future.addActionListener(new ReservationsListController(this));
		
		ButtonGroup group = new ButtonGroup();
		
		group.add(all);
		group.add(past);
		group.add(current);
		group.add(future);
		
		toggleButtonsPanel.add(all);
		toggleButtonsPanel.add(past);
		toggleButtonsPanel.add(current);
		toggleButtonsPanel.add(future);
				
		return toggleButtonsPanel;
	}

/*	public static void main(String[] args){		
		ReservationListView layout = new ReservationListView();		
		JFrame frame = new JFrame();
		JPanel panel = layout.getMainPanel();
		frame.add(panel);
		frame.setSize(400, 200);
		frame.setVisible(true);
	}*/

	public DefaultListModel<Reservation> getModel() {
		return model;
	}

	public void setModel(DefaultListModel<Reservation> model) {
		this.model = model;
	}
}
